package net.expectx.pay.controller.merchant;

import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.base.BaseConstants;
import net.expectx.common.single.base.BaseResult;
import net.expectx.pay.common.AdminConstants;
import net.expectx.pay.common.BaseController;
import net.expectx.pay.dao.bean.MerchantUser;
import net.expectx.pay.dao.bean.MerchantUserExample;
import net.expectx.pay.dao.bean.MerchantUser;
import net.expectx.pay.dao.bean.MerchantUserExample;
import net.expectx.pay.rpc.api.MerchantUserService;
import net.expectx.pay.rpc.api.MerchantUserService;
import net.expectx.pay.rpc.api.SystemTypeGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2018/09/06
 *
 * @author Rui
 */
@Controller
@RequestMapping(value = "/manager/merchant/user")
public class MerchantUserController extends BaseController {
    @Autowired
    private SystemTypeGroupService systemTypeGroupService;
    @Autowired
    private MerchantUserService merchantUserService;

    @ApiOperation(value = "认证主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        MerchantUserExample merchantUserExample = new MerchantUserExample();
        MerchantUserExample.Criteria criteria2 = merchantUserExample.createCriteria();
        criteria2.andIsDeletedEqualTo(BaseConstants.NO);
        List<MerchantUser> merchantUsers = merchantUserService.selectByExample(merchantUserExample);
        modelMap.put("merchantUsers", merchantUsers);
        return "merchant/user/index";
    }


    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object page(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>(16);
        String merchantNo = request.getParameter("merchantNo");

        MerchantUserExample merchantUserExample=new MerchantUserExample();
        /**
         * 创造条件
         */
        MerchantUserExample.Criteria criteria= merchantUserExample.createCriteria();
        if(StringUtils.isNotBlank(merchantNo)){
            criteria.andMerchantNoEqualTo(merchantNo);
        }
        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        /**
         * 分页
         */
        merchantUserExample.setPageIndex(page);
        merchantUserExample.setPageSize(limit);
        /**
         * 排序
         */
        merchantUserExample.setOrderByClause("gmt_modified desc");

        List<MerchantUser> merchantUsers = merchantUserService.selectByExample(merchantUserExample);
        long total = merchantUserService.countByExample(merchantUserExample);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", merchantUsers);
        return result;
    }



}
